//运行在沙盒上的NODEJS 预加载JS

window._send=function(funcname,...args){
    postMessage({
        target:"preload",
        funcname:funcname,
        args:args
    },location.origin)
}

window.addEventListener('DOMContentLoaded', () => {

    // const replaceText = (selector, text) => {
    //   const element = document.getElementById(selector)
    //   if (element) element.innerText = text
    // }
    // for (const type of ['chrome', 'node', 'electron']) {
    //   replaceText(`${type}-version`, process.versions[type])
    // }

    const { ipcRenderer } = require('electron')
    window.ipcRenderer = ipcRenderer
    window.call_list={}
    window.call_timeout={}

    ipcRenderer.on("save_or_open_file_notify",function(event,data){
        if(window.call_list["save_or_open_file_notify"]){
            window._send("save_or_open_file_notify","success",data)
            window.call_list["save_or_open_file_notify"]=null
        }
    })
    ipcRenderer.on("exists_file_notify",function(event,data){
        if(window.call_list["exists_file_notify"]){
            window._send("exists_file_notify","success",data)
            window.call_list["exists_file_notify"]=null
        }
    })


    ipcRenderer.on("load_db",function(event,data){
        if(window.call_list["load_db"]){
            clearTimeout(window.call_timeout["load_db"])
            window._send("load_db","success",data)
            window.call_list["load_db"]=null
        }
    })
    ipcRenderer.on("load_note",function(event,row,err){
        if(window.call_list["load_note"]){
            clearTimeout(window.call_timeout["load_note"])
            window._send("load_note","success",row,err)
            window.call_list["load_note"]=null
        }
    })
    ipcRenderer.on("set_notebook_notify",function(event,err){
        if(window.call_list["set_notebook_notify"]){
            clearTimeout(window.call_timeout["set_notebook_notify"])
            window._send("set_notebook_notify","success",err)
            window.call_list["set_notebook_notify"]=null
            clearTimeout(window.call_timeout["load_note"])
        }
    })
    ipcRenderer.on("set_note_notify",function(event,err){
        if(window.call_list["set_note_notify"]){
            clearTimeout(window.call_timeout["set_note_notify"])
            window._send("set_note_notify","success",err)
            window.call_list["set_note_notify"]=null
        }
    })
    ipcRenderer.on("rem_note_notify",function(event,err){
        if(window.call_list["rem_note_notify"]){
            clearTimeout(window.call_timeout["rem_note_notify"])
            window._send("rem_note_notify","success",err)
            window.call_list["rem_note_notify"]=null
        }
    })
    ipcRenderer.on("rem_notebook_notify",function(event,err){
        if(window.call_list["rem_notebook_notify"]){
            clearTimeout(window.call_timeout["rem_notebook_notify"])
            window._send("rem_notebook_notify","success",err)
            window.call_list["rem_notebook_notify"]=null
        }
    })
    ipcRenderer.on("set_note_text_notify",function(event,err){
        if(window.call_list["set_note_text_notify"]){
            clearTimeout(window.call_timeout["set_note_text_notify"])
            window._send("set_note_text_notify","success",err)
            window.call_list["set_note_text_notify"]=null
        }
    })

    window.preload_Caller = {

        //超时处理
        _timeout:function(duration,fq){
            window.call_timeout[fq] = setTimeout(() => {
                if(window.call_list[fq]){
                    window.call_list[fq]("timeout",null)
                    window.call_list[fq]=null
                }
            }, duration ? duration : 4000);
        },
        open_file:function(callback){
            window.call_list["save_or_open_file_notify"]={}
            ipcRenderer.invoke('app','save_or_open_file')
        },
        exists_file:function(filepath,callback){
            window.call_list["exists_file_notify"]={}
            ipcRenderer.invoke('app','exists_file',filepath)
        },
        select_db:function(db_filepath,callback,timeout){
            ipcRenderer.invoke('app','select_db',db_filepath)
            window.call_list["load_db"]={}
            this._timeout(timeout,"load_db")
        },
        search_db:function(keyword,callback,timeout){
            ipcRenderer.invoke('app','search_db',keyword)
            window.call_list["load_db"]={}
            this._timeout(timeout,"load_db")
        },
        get_note:function(note_id,callback,timeout){
            ipcRenderer.invoke('app','get_note',note_id)
            window.call_list["load_note"]={}
            this._timeout(timeout,"load_note")
        },
        set_note:function(arg,callback,timeout){
            ipcRenderer.invoke('app','set_note',{
                notebook_id:arg.notebook_id,
                note_id:arg.note_id,
                name:arg.name
            })
            window.call_list["set_note_notify"]={}
            this._timeout(timeout,"set_note_notify")
        },  
        set_note_text:function(note_id,text,callback,timeout){
            ipcRenderer.invoke('app','set_note_text',note_id,text)
            window.call_list["set_note_text_notify"]={}
            this._timeout(timeout,"set_note_text_notify")
        },
        set_notebook:function(arg,callback,timeout){
            ipcRenderer.invoke('app','set_notebook',{
                notebook_id:arg.notebook_id,
                parent_id:arg.parent_id,
                name:arg.name
            })
            window.call_list["set_notebook_notify"]={}
            this._timeout(timeout,"set_notebook_notify")
        },
        rem_note:function(note_id,callback,timeout){
            ipcRenderer.invoke('app','rem_note',note_id)
            window.call_list["rem_note_notify"]={}
            this._timeout(timeout,"rem_note_notify")
        },
        rem_notebook:function(notebook_id,callback,timeout){
            ipcRenderer.invoke('app','rem_notebook',notebook_id)
            window.call_list["rem_notebook_notify"]={}
            this._timeout(timeout,"rem_notebook_notify")
        }
    }
    window.addEventListener("message",function(event){
        if(event.data.target=="call"){
            if(event.data.funcname){
                if(window.preload_Caller[event.data.funcname]){
                    window.preload_Caller[event.data.funcname](...event.data.args)
                }
            }
        }
    })

})
